package com.leeoohoo.config;

import com.leeoohoo.common.UserContext;
import com.leeoohoo.common.UserHolder;
import com.oohoo.spacestationspringbootstarter.dto.query.intercept.CreateByInit;
import com.oohoo.spacestationspringbootstarter.dto.query.intercept.LastUpdateByInit;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Objects;

@Component
public class QueryInitConfig {


    public static String currentUsername() {
        UserContext myUser = UserHolder.getMyUser();

        if (Objects.isNull(myUser)) {
            return "sys";
        }
        return myUser.getUsername();
    }

    @Bean
    public CreateByInit getCreateByInit() {
        return QueryInitConfig::currentUsername;
    }

    @Bean
    public LastUpdateByInit getInsertOrUpdateIntercept() {
        return new LastUpdateByInit() {
            @Override
            public Object current() {
                return currentUsername();
            }

            public LocalDateTime time() {
                return LocalDateTime.now();
            }

        };
    }
}
